/* Replication code for the following paper: Exley, Christine L., Nils H. Lehr, and Stephen J. Terry. "Nonprofits in Good Times and Bad Times", Journal of Political Economy Microeconomics, accepted in 2022	
*/

***************************************************************
************************************************

* LOCALS 
local raw_class "FINAL_classifications.dta" 
local a1 "REGDATA_EIN.dta"
local b1 "REGDATA_SOI.dta"
local c1 "REGDATA_COMPUSTAT.dta" 

set scheme s2mono

ssc install mhtexp
ssc install sxpose
ssc install reghdfe
ssc install spmap
ssc install  shp2dta



************************************************************
************************************************************
*** Compute List MHT p-values for DCR comparisons by subgroup
*** Table C.6

clear 
use `raw_class'

drop _merge score rank* ntee_description ntee_definiiton sample_size loop

gen score0 = score_inc_no
gen score1 = score_inc_yes

egen ntee_major_code = group(ntee_major)

reshape long score, i(ntee) j(incent)

drop if missing(ntee)
drop score_*

*create baseline t-stat for difference between incentived and unincentivized
reg score i.incent
mhtexp score, treatment(incent)

*create t-stat's by ntee_major for difference between incentived and unincentivized
mhtexp score, treatment(incent) subgroup(ntee_major_code)

version 15: table ntee_major


************************************************************
************************************************************
*** LARGE DATA: Statistics for 2000/2013 (used in text)


use rev ass_boy ein year using `a1' if inlist(year,2000,2013), clear

replace rev = 0 if rev < 0

rename ass_boy ass
replace ass = 0 if ass < 0

gcollapse(sum) rev ass (count) firms = rev, by(year)

replace rev = rev

* - All data from Stephen's spreadsheet -
gen firms_subs = 5652544 if year == 2000
replace firms_subs = 5775055 if year == 2013

gen gdp = 1000000*10252347 if year == 2000
replace gdp = 16784851000000 if year == 2013

gen rev_pub = 12795574.12884*1000000 if year == 2013
gen ass_pub = 12795574.12884*1000000 if year == 2013

gen rev_mfg = 4917104.49433*1000000 if year == 2013
gen ass_mfg = 5790223.497648*1000000 if year == 2013

foreach var in rev ass {
	foreach stub in mfg pub {
		replace `var'_`stub' = `var'/`var'_`stub'*100
	}
}

replace rev = rev/gdp*100
replace firms = firms/(firms+firms_subs)*100

drop ass

keep year rev* ass* firms

foreach var of varlist rev* ass* firms {
	tostring `var', replace format(%9.1fc) force
	replace `var' = `var'+"\%"
}

tostring year, replace force

sxpose, clear

replace _var1 = "" if _var1 == ".\%"

gen _var0 = "Year"
replace _var0 = "Nonprofit revenue to GDP" if _n == 2
replace _var0 = "Nonprofit firms to all firms" if _n == 3
replace _var0 = "Nonprofit to listed revenue" if _n == 4
replace _var0 = "Nonprofit to listed assets" if _n == 5
replace _var0 = "Nonprofit to listed manufacturing revenue" if _n == 6
replace _var0 = "Nonprofit to listed manufacturing assets" if _n == 7

order _var0 _var1 _var2

set obs 10

capture drop text
gen text = ""

replace text = "\begin{tabular}{l c c } \toprule \toprule" if _n == 1

forvalues i = 1/7 {
	replace text = _var0[`i'] if _n == (`i'+1)
}

foreach var in _var1 _var2 {
	forvalues i = 1/7 {
		replace text = text + " & " + `var'[`i'] if _n == (`i'+1)
	}	
}
replace text = text + " \\" if inrange(_n,2,8)
replace text = text + " \midrule" if _n == 2
replace text = "\bottomrule \bottomrule" if _n == 9
replace text = "\end{tabular}" if _n == 10

outsheet text using footnote4_stats.tex, replace noquote nonames nolabel


************************************************************
************************************************************
*** LARGE DATA: Summary statistics for growth rates/ levels
*** Table A.4

clear 
clear matrix
use `a1'

drop if missing(tercile)|missing(level1)|missing(ntmaj5) // Drop obs with missing cat. variables

* - Summary Statistics -
estpost tabstat exp_dhs rev_dhs ass_dhs liab_dhs con_dhs noncon_dhs perinc_dhs perinc_nat_dhs emp_dhs wage_dhs urate, s(me sd p50 iqr) columns(statistics)

esttab  using summary_statistics_growth.tex, ///
	cell("mean(fmt(%9.3f)) sd(fmt(%9.3f)) p50(fmt(%9.3f)) iqr(fmt(%9.3f))") ///
	replace label substitute("\_" "_") style(tex)


* - Summary Statistics -
rename ass_eoy ass
rename liab_eoy liab

* Selection 
foreach var in rev exp ass {
	drop if `var' <= 0
	replace `var' = log(`var')
}

replace liab = log(liab)

lab var rev  "Revenue"
lab var exp  "Expenditure"
lab var ass  "Assets"
lab var liab "Liabilities"


estpost tabstat exp rev ass liab perinc perinc_nat, s(me sd p50 iqr) columns(statistics)

esttab  using summary_statistics_level.tex, ///
	cell("mean(fmt(%9.3f)) sd(fmt(%9.3f)) p50(fmt(%9.3f)) iqr(fmt(%9.3f))") ///
	replace label substitute("\_" "_") style(tex)

************************************************************
************************************************************
*** SMALL DATA: Summary statistics
*** Table A.5

clear 
clear matrix
use `b1'

* - Summary Statistics -
estpost tabstat rev_tot_dhs exp_tot_dhs Dexp_prog_share exp_prog_dhs exp_gen_dhs rev_don_dhs rev_nondon_dhs perinc_dhs perinc_nat_dhs, s(me sd p50 iqr) columns(statistics)

esttab  using soi_summary_statistics.tex, ///
	cell("mean(fmt(%9.3f)) sd(fmt(%9.3f)) p50(fmt(%9.3f)) iqr(fmt(%9.3f))") ///
	replace label substitute("\_" "_") style(tex)

* - Summary Statistics -
rename ass_eoy ass
rename liab_eoy liab
rename exp_tot exp
rename rev_tot rev

* Selection 
foreach var in rev exp ass {
	drop if `var' <= 0
	replace `var' = log(`var')
}

replace liab = log(liab)

lab var rev  "Revenue"
lab var exp  "Expenditure"
lab var ass  "Assets"
lab var liab "Liabilities"


estpost tabstat exp rev ass liab perinc perinc_nat, s(me sd p50 iqr) columns(statistics)

esttab  using summary_statistics_level_soi.tex, ///
	cell("mean(fmt(%9.3f)) sd(fmt(%9.3f)) p50(fmt(%9.3f)) iqr(fmt(%9.3f))") ///
	replace label substitute("\_" "_") style(tex)

************************************************************
************************************************************
*** FOR PROFIT DATA: Summary statistics
*** Table A.6

clear 
clear matrix
use `c1'

* - Generate consistent regdata sample -
qui reghdfe rev_dhs exp_dhs perinc_dhs , absorb(state#year gvkey)
keep if e(sample)

eststo clear

* - Summary Statistics -
estpost tabstat exp_dhs rev_dhs ass_dhs liab_dhs perinc_nat_dhs perinc_dhs, s(me sd p50 iqr) columns(statistics)

esttab  using compustat_summary_statistics.tex, ///
	cell("mean(fmt(%9.3f)) sd(fmt(%9.3f)) p50(fmt(%9.3f)) iqr(fmt(%9.3f))") ///
	replace label substitute("\_" "_") style(tex)	


************************************************************
************************************************************
*** LARGE DATA: Regressions 
*** Tables 2, 4, 5, 6, B.2

foreach d in exp_dhs rev_dhs ass_dhs liab_dhs con_dhs noncon_dhs { 
	
clear 
clear matrix
use `a1'

local rev_dhsL "Change in Revenue"
local exp_dhsL "Change in Expenditure"
local ass_dhsL  "Change in Assets" 
local liab_dhsL  "Change in Liabilities"
local con_dhsL  "Change in Contributions"
local noncon_dhsL  "Change in Non-Contributions"

*** MAIN REGRESSIONS 
foreach s in all top10 { 
	
*fix the sample to a uniform value
qui: reghdfe `d' perinc_dhs if `s'==1, absorb(ein year) cluster(statefip)
	
** NATIONAL  
qui: reghdfe `d' perinc_nat_dhs ///
	if `s' ==1 ///
	,noabsorb  cluster(statefip ) nosample
eststo  a`s'

** LOCAL  
qui: reghdfe `d' perinc_dhs ///
	if `s' ==1 ///
	,noabsorb  cluster(statefip ) nosample
eststo  b`s'

** LOCAL  + Year FEs
qui: reghdfe `d' perinc_dhs ///
	if `s' ==1 ///
	,absorb(year)  cluster(statefip ) nosample
eststo  c`s'

}

	
 esttab aall ball call  ///
	atop10 btop10 ctop10  ///
  	using reg_`d'.tex /// 
 	, b(2) se(2)  /// 
	title("OLS of ``d'L'  ") /// 
	 mgroups(  "All" "Top 10 percent"   , pattern(1 0 0  1 0 0   ) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  /// 
	mtitles("No FE" "No FE" "Y FE"  "No FE" "No FE" "Y FE"  ) ///
	nogaps  compress  nocons /// 
	 label    /// 
 	 star(* 0.10 ** 0.05 *** 0.01 )  /// 
	addnote("SEs clustered at statefip")  replace
	}



******************************************************
******************************************************
**** LARGE DATA: HETEROGENEITY 


foreach s in all  { 
foreach i in  perinc_dhs { 
foreach d in ass_dhs  liab_dhs   exp_dhs rev_dhs   { 

clear 
use `a1'

local rev_dhsL "Change in Revenue"
local exp_dhsL "Change in Expenditure"
local ass_dhsL  "Change in Assets" 
local liab_dhsL  "Change in Liabilities"

keep if `s' == 1 



*************************************
***  decile_conintensity
*** Table B.11

foreach v in 1 2 3 4 5 6 7 8 9 10  { 	
reghdfe `d'  `i' ///
	if  decile_con_intensity ==`v' ///
	,noabsorb  cluster(statefip ) nosample
eststo  d`v'
}

esttab   d1 d2 d3 d4 d5 d6 d7 d8 d9 d10  ///
  	using regByConInt_`d'_`i'_`s'.tex /// 
 	, b(2) se(2)  /// 
	title("`s',  By Contribution Intensity, OLS of ``d'L'") /// 
		mgroups(  "By decile of contribution intensity:"   , pattern(1  0 0 0 0 0 0  0 0 0  ) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  /// 
	mtitles("1" "2" "3" "4" "5" "6" "7" "8" "9" "10" ) ///
	nogaps  compress  nocons /// 
	label    /// 
 	star(* 0.10 ** 0.05 *** 0.01 )  /// 
	addnote("No FEs, SEs clustered at statefip")  replace



*************************************
***  SCORE
*** Table B.4

label variable score_dm "DCR"  
label variable score_dmXperinc_dhs "DCR*\$\Delta\$ CZ income"  

foreach v in 1 2 3 4 5 6 7 8 9 10  { 	
qui: reghdfe `d'  `i' ///
	if  score_10q ==`v' ///
	,noabsorb  cluster(statefip ) nosample
eststo  d`v'
}

qui: reghdfe `d'  `i' score_dm score_dmX`i'  ///
	,noabsorb  cluster(statefip ) nosample
eststo  dall

esttab  dall d1 d2 d3 d4 d5 d6 d7 d8 d9 d10  ///
  	using regByScore_`d'_`i'_`s'.tex /// 
 	, b(2) se(2)  /// 
	title("`s',  By Score Decile, OLS of ``d'L'") /// 
		mgroups(  "All" "By score decile:"   , pattern(1 1 0 0 0 0 0 0  0 0 0  ) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  /// 
	mtitles("All" "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" ) ///
	nogaps  compress  nocons /// 
	label    /// 
 	star(* 0.10 ** 0.05 *** 0.01 )  /// 
	addnote("No FEs, SEs clustered at statefip")  replace
*********************************
*** SIZE 
*** Table B.5 & B.6

local v1 "Size Decile = 1"
local v2 "Size Decile = 2"
local v3 "Size Decile = 3"
local v4 "Size Decile = 4"
local v5 "Size Decile = 5"
local v6 "Size Decile = 6"
local v7 "Size Decile = 7"
local v8 "Size Decile = 8"
local v9 "Size Decile = 9"
local v10 "Size Decile = 10"

foreach v in 1 2 3 4 5 6 7 8 9 10 { 
qui: reghdfe `d'  `i' ///
	if  decile ==`v' ///
	,noabsorb  cluster(statefip ) nosample
eststo  d`v'

qui: reghdfe `d'  `i' score_dm score_dmX`i' /// 
	if  decile ==`v' ///
	,noabsorb  cluster(statefip ) nosample
eststo  d`v'2
}

esttab  d1 d2 d3 d4 d5 d6 d7 d8 d9 d10 /// 
  	using regBySize_`d'_`i'_`s'.tex /// 
 	, b(2) se(2)  /// 
	title("`s',  By Size Decile, OLS of ``d'L'") /// 
	mtitles( "d=1" "d=2" "d=3" "d=4" "d=5" "d=6" "d=7" "d=8" "d=9" "d=10" ) ///
	nogaps  compress  nocons /// 
	label    /// 
 	star(* 0.10 ** 0.05 *** 0.01 )  /// 
	addnote("No FEs, SEs clustered at statefip")  replace

esttab  d12 d22 d32 d42 d52 d62 d72 d82 d92 d102 /// 
  	using regBySizeXScore_`d'_`i'_`s'.tex /// 
 	, b(2) se(2)  /// 
	title("`s',  By Size Decile, OLS of ``d'L'") /// 
		mgroups( "By Size Decile"   , pattern(1 0 0 0 0 0 0  0 0 0  ) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  /// 
	mtitles("All" "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" ) ///
	nogaps  compress  nocons /// 
	label    /// 
 	star(* 0.10 ** 0.05 *** 0.01 )  /// 
	addnote("No FEs, SEs clustered at statefip")  replace	

******************************************
*** 12 Major Codes 
*** Table B.7

local v7 "Human Services" 
local v3 "Higher Education" 
local v2 "Education" 
local v6 "Hospital" 
local v5 "Health" 
local v1 "Arts, Culture, and Humanities" 
local v11 "Religion" 
local v4 "Environment" 
local v9 "Mutual Benefit" 
local v10 "Public or societal benefiti" 
local v8 "International" 



foreach v in 1 2 3 4 5 6 7 8 9 10 11   { 
qui: reghdfe `d'  `i' ///
	if ntmaj12 ==`v' ///
	,noabsorb  cluster(statefip ) nosample
eststo  d`v'
}

esttab  d7 d3  d2    d6  d5   d1  d11 d4   d9 d10 d8    /// 
  	using regByMajor_`d'_`i'_`s'.tex /// 
 	, b(2) se(2)  /// 
	title("`s',  By Major Group, OLS of ``d'L'") /// 
	mtitles("Hum serv"   "High ed" "Educ"  "Hosp"  "Health"  "Arts"   "Relig"   "Envir."  "Mut ben" "Pub/soc ben" "Intnat"   ) ///
	nogaps  compress  nocons /// 
	 label    /// 
 	 star(* 0.10 ** 0.05 *** 0.01 )  /// 
	addnote("No FEs, SEs clustered at statefip")  replace


******************************************
*** Source 
*** Table B.8

foreach v in 1 2 3   { 

qui: reghdfe `d'  `i' ///
	if source ==`v' ///
	,noabsorb  cluster(statefip ) nosample
eststo  d`v'
}
esttab  d1 d2 d3    ///
  	using regBySource_`d'_`i'_`s'.tex /// 
 	, b(2) se(2)  /// 
	title("`s',  By Legal Form, OLS of ``d'L'") /// 
	mtitles("Public Charities" "Private Foundations" "Other" ) ///
	nogaps  compress  nocons /// 
	 label    /// 
 	 star(* 0.10 ** 0.05 *** 0.01 )  /// 
		addnote("No FEs, SEs clustered at statefip")  replace

*************************************
*** Region 
*** Table B.9

foreach v in 1 2 3 4  { 

qui: reghdfe `d'  `i' ///
	if region ==`v' ///
	,noabsorb  cluster(statefip ) nosample
eststo  d`v'
}
esttab  d1 d2 d3 d4    ///
  	using regByRegion_`d'_`i'_`s'.tex /// 
 	, b(2) se(2)  /// 
	title("`s',  By Region, OLS of ``d'L'") /// 
	mtitles("Northeast" "Midwest" "South" "West" ) ///
	nogaps  compress  nocons /// 
	 label    /// 
 	 star(* 0.10 ** 0.05 *** 0.01 )  /// 
		addnote("No FEs, SEs clustered at statefip")  replace
	
*************************************
***  urban_index
*** Table B.10

local v1 "Urban" 
local v2 "Rural" 
local v3 "Intermediate" 

foreach v in 1 2 3   { 
qui: reghdfe `d'  `i' ///
	if urban_index ==`v' ///
	,noabsorb  cluster(statefip ) nosample
eststo  d`v'
}
esttab  d1 d2 d3    ///
  	using regByUrban_`d'_`i'_`s'.tex /// 
 	, b(2) se(2)  /// 
	title("`s', By Urban, OLS of ``d'L'") /// 
	mtitles("Urban" "Rural" "Intermediate" ) ///
	nogaps  compress  nocons /// 
	 label    /// 
 	 star(* 0.10 ** 0.05 *** 0.01 )  /// 
		addnote("No FEs, SEs clustered at statefip")  replace	

}	
}
}


******************************************************
******************************************************
**** LARGE DATA: Other cyclicality measures  
*** Table B.12

foreach s in all  { 
foreach d in   exp_dhs rev_dhs  ass_dhs  liab_dhs { 

clear 
use `a1'

local rev_dhsL "Change in Revenue"
local exp_dhsL "Change in Expenditure"
local ass_dhsL  "Change in Assets" 
local liab_dhsL  "Change in Liabilities"

keep if `s' == 1 

*************************************
***  Cyclicality measure 

foreach i in perinc_dhs incpc_dhs emp_dhs wage_dhs urate { 
qui: reghdfe `d'  `i' ///
	,noabsorb  cluster(statefip ) nosample 
eststo  d`i'
}
esttab  dperinc_dhs dincpc_dhs demp_dhs dwage_dhs durate    ///
  	using regByOtherMeasures_`d'_`s'.tex /// 
 	, b(2) se(2)  ar2(4)  /// 
	title("`s', By Other Cylicality Measures, OLS of ``d'L'") /// 
	mtitles("personal income" "personal income p.c." "employment" "wage" "unemployment" ) ///
	nogaps  compress  nocons /// 
	 label    /// 
 	 star(* 0.10 ** 0.05 *** 0.01 )  /// 
		addnote("No FEs, SEs clustered at statefip")  replace
}
}	

	
	
	
************************************************************
************************************************************
*** LARGE DATA: LEVEL Regressions 
*** Table B.13

foreach d in exp rev ass liab { 

clear 
clear matrix
use `a1'

rename ass_eoy ass
rename liab_eoy liab

* Selection 
foreach var in rev exp ass {
	drop if `var' <= 0
}

drop if missing(tercile)|missing(level1)|missing(ntmaj5) // Drop obs with missing cat. variables

gen linear = year-1990
lab var linear "Linear trend"

replace `d' = log(`d')

local revL "Log Revenue "
local expL "Log Expenditure"
local assL  "Log Assets" 
local liabL  "Log Liabilities"

gstats winsor exp rev ass liab perinc perinc_nat, cuts(1 99) replace by(year)

*** MAIN REGRESSIONS 
foreach s in all top10 { 
	
*fix the sample to a uniform value
qui: reghdfe `d' perinc if `s'==1, absorb(ein year#state) cluster(statefip)
	
** NATIONAL  
qui: reghdfe `d' perinc_nat linear ///
	if `s' ==1 ///
	,noabsorb  cluster(statefip ) nosample
eststo  a`s'

** LOCAL  
qui: reghdfe `d' perinc linear ///
	if `s' ==1 ///
	,noabsorb  cluster(statefip ) nosample
eststo  b`s'

** LOCAL  + Year FEs
qui: reghdfe `d' perinc ///
	if `s' ==1 ///
	,absorb(year)  cluster(statefip ) nosample
eststo  c`s'

}

esttab aall ball call  ///
	atop10 btop10 ctop10  ///
  	using reg_level_`d'_log.tex /// 
 	, b(2) se(2)  /// 
	title("OLS of ``d'L'  ") /// 
	 mgroups(  "All" "Top 10 percent"   , pattern(1 0 0  1 0 0   ) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  /// 
mtitles("No FE" "No FE" "Y FE"  "No FE" "No FE" "Y FE"  ) ///
	nogaps  compress  nocons /// 
	 label    /// 
 	 star(* 0.10 ** 0.05 *** 0.01 )  /// 
	addnote("SEs clustered at statefip")  replace
}


************************************************************
************************************************************
*** SMALL DATA: Regressions 
*** Tables 3, B.1, B.3

foreach d in   exp_prog_dhs exp_gen_dhs Dexp_prog_share rev_don_dhs rev_nondon_dhs { 

clear 
clear matrix
use `b1'

local Dexp_prog_shareL "Change in Program Expenditure Share"
local rev_don_dhsL "Change in Donations"
local rev_nondon_dhsL "Change in Non-Donation Revenue"
local exp_gen_dhsL "Change in Administrative Expenditure"
local exp_prog_dhsL "Change in Program Expenditure"

*** MAIN REGRESSIONS 
foreach s in all top10 { 
	
	
*fix the sample to a uniform value
qui: reghdfe `d' perinc_dhs if `s'==1, absorb(ein year) cluster(statefip)
		
	
** NATIONAL  
qui: reghdfe `d' perinc_nat_dhs ///
	if `s' ==1 ///
	,noabsorb  cluster(statefip )
eststo  a`s'

** LOCAL  
qui: reghdfe `d' perinc_dhs ///
	if `s' ==1 ///
	,noabsorb  cluster(statefip )
eststo  b`s'

** LOCAL  + Year FEs
qui: reghdfe `d' perinc_dhs ///
	if `s' ==1 ///
	,absorb(year)  cluster(statefip )
eststo  c`s'

}


 esttab aall ball call  ///
	atop10 btop10 ctop10  ///
  	using "reg_`d'.tex" /// 
 	, b(2) se(2)  /// 
	title("OLS of ``d'L'  ") /// 
	 mgroups(  "All" "Top 10 percent"   , pattern(1 0 0  1 0 0   ) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  /// 
	mtitles("No FE" "No FE" "Y FE"  "No FE" "No FE" "Y FE"  ) ///
	nogaps  compress  nocons /// 
	 label    /// 
 	 star(* 0.10 ** 0.05 *** 0.01 )  /// 
	addnote("SEs clustered at statefip")  replace
} 


************************************************************
************************************************************
*** FOR PROFIT DATA: Main Regressions 
*** Table B.14

foreach d in  rev_dhs  exp_dhs  ass_dhs  liab_dhs { 
clear 
clear matrix
use `c1'

local rev_dhsL "Change in Revenue"
local exp_dhsL "Change in Expenditure"
local ass_dhsL  "Change in Assets" 
local liab_dhsL  "Change in Liabilities"

gen ein = gvkey
gen all = 1

*** MAIN REGRESSIONS 
foreach s in all  { 
	
** NATIONAL  
qui: reghdfe `d' perinc_nat_dhs ///
	if `s' ==1 ///
	,noabsorb  cluster(statefip ) nosample
eststo  a`s'

** LOCAL  
qui: reghdfe `d' perinc_dhs ///
	if `s' ==1 ///
	,noabsorb  cluster(statefip ) nosample
eststo  b`s'

** LOCAL  + Year FEs
qui: reghdfe `d' perinc_dhs ///
	if `s' ==1 ///
	,absorb(year)  cluster(statefip ) nosample
eststo  c`s'

}

	
 esttab aall ball call  ///
  	using reg_forprofit_`d'.tex /// 
 	, b(2) se(2)  /// 
	title("OLS of ``d'L'  ") /// 
	 mgroups(  "All"    , pattern(1 0 0  ) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  /// 
	mtitles("No FE" "No FE" "Y FE"   ) ///
	nogaps  compress  nocons /// 
	 label    /// 
 	 star(* 0.10 ** 0.05 *** 0.01 )  /// 
	addnote("SEs clustered at statefip")  replace
		
}
*/
************************************************************
************************************************************
*** MAP 
*** Figure 9

clear 
use `a1'
 
keep if year == 2010
drop if statefip == 15| statefip == 2 // Get rid of Hawaii and Alaska to Improve Visual

gcollapse(sum) exp (mean) pop, by(CZ90)

foreach var in exp {
	replace `var' = `var'/pop
} 

tempfile temp_data
save `temp_data'

* - Load shapefile & merge with data -
shp2dta using `map'cz1990.shp, ///
database(`map'czdb) coordinates(`map'czcoord) genid(id) replace

use `map'czdb,clear

rename cz CZ90

joinby CZ90 using `temp_data'
 
* - Expenditures -
spmap exp using `map'czcoord , id(id) name(expenditures,replace) fcolor(Greys) clmethod(custom) clbreaks(0 250 500 1000 2500 5000 10000 20000 35000) gsize(4) ///
ndfcolor(black) legend(title("Expenditure p.c.",size(medlarge)) size(medlarge) pos(5)) title("") legstyle(2)

gr export map_expenditures_2010.eps,replace //width(1500) 
gr export map_expenditures_2010.png,replace width(1500) 
erase `map'czcoord.dta
erase `map'czdb.dta
gr close

************************************************************
************************************************************
*** Descriptive figures for heterogeneity 

clear 
use `a1'

 
keep if  year == 2010 & !missing(ntmaj5) & !missing(level1)

gen obs = 1
replace ass_boy = 0 if ass_boy < 0

*****Pie chart for major groups 
*** Figure 7

gr pie obs ///
	, name(obs,replace) over(ntmaj5) sort descending ang(315) ///
	plabel(_all percent, format(%9.1fc) color(white)) pie(_all, explode(1)  ) ///
	legend(off) line(lcolor(black) lwidth(0.05)) ///
	ptext(140 68 "Human Services"  "e.g. Greater Lansing" "Food Bank",  nobox) ///
	ptext(215 68 "Other"  "e.g. Junior League" "of Boca Raton",  nobox) ///
	ptext(333 68 "Arts, Culture," "& Humanities"  "e.g. Korean School" "of Orange County", nobox) ///
	ptext(358 68 "Health"  "e.g. Georgia" "Meth Project", nobox) ///
	ptext(32 72 "Education" "e.g. Kentucky Coalition" "for Literacy", nobox) ///
	graphregion(color(white)) bgcolor(white) 


gr combine obs ///
	, imargin(medium) name(obs,replace) ///
	graphregion(color(white)) 
	
gr export pie_major_2010.eps, replace
gr export pie_major_2010.pdf, replace
gr close

*** Des stats table major groups 
forvalues i = 1(1)5{
g ntmaj5_`i' = 0
replace ntmaj5_`i' =1 if ntmaj5==`i'
}

forvalues i = 1(1)11{
g ntmaj12_`i' = 0
replace ntmaj12_`i' =1 if ntmaj12==`i'
}

label variable ntmaj12_1 "Arts, Culture, and Humanities" 
label variable ntmaj12_2 "Education" 
label variable ntmaj12_3 "Higher Education" 
label variable ntmaj12_4 "Environment" 
label variable ntmaj12_5 "Health" 
label variable ntmaj12_6 "Hospital" 
label variable ntmaj12_7 "Human Services" 
label variable ntmaj12_8 "International" 
label variable ntmaj12_9 "Mutual Benefit" 
label variable ntmaj12_10 "Public or societal benefit" 
label variable ntmaj12_11 "Religion"

label variable ntmaj5_1 "Major 5: Arts, Culture, and Humanities"
label variable ntmaj5_2 "Major 5:Education" 
label variable ntmaj5_3 "Major 5: Health"
label variable ntmaj5_4 "Major 5: Human Services"
label variable ntmaj5_5 "Major 5: Other"

eststo: quietly estpost summarize ntmaj12_* ntmaj5_*, listwise

esttab using des_major.tex ///
	, cells("mean") title("Distribution of 11 major categories") ///
	nonumber label replace 
	
*****Bar chart for size 
*** Figure 6

gen size_class = 1 if inrange(ass_boy,0,50000)
replace size_class = 2 if inrange(ass_boy,50*10^3,2.5*10^5)
replace size_class = 3 if inrange(ass_boy,2.5*10^5,10^6)
replace size_class = 4 if inrange(ass_boy,10^6,5000000)
replace size_class = 5 if inrange(ass_boy,5000000,10^8)
replace size_class = 6 if inrange(ass_boy,10^8,10^12)

gen obs2 = 1/1000

gr bar (percent) obs2 ///
	, over(size_class,relabel(1 `""Below" "50k""' 2 "50k - 250k" 3 "250k - 1m" 4 "1m - 5m" 5 "5m - 100m" 6 `" "More than" "100m" "' )) ///
	ytitle("Percent of Non-profits") blabel(total,format(%9.1fc)) ///
	bar(1,color(gray%50) lcolor(black))  ///
	graphregion(color(white)) bgcolor(white) 

	gr export bar_assets_2010.eps, replace
gr export bar_assets_2010.pdf, replace
gr close

*****Pie chart for LEGAL FORMS 
*** Figure 8

gr pie obs ///
	, name(obs,replace) over(level1) sort descending ang(290) ///
	plabel(_all percent, format(%9.1fc) color(white)) pie(_all, explode(1) ) ///
	legend(off) line(lcolor(black) lwidth(0.05)) ///
	ptext(180 72 "Public Charities"  "e.g. So All May Eat", nobox) ///
	ptext(30 74 "Other"  "e.g. Mansfield" "YMCA/YWCA", nobox) ///
	ptext(330 74 "Private Foundations"  "e.g. Little Star Foundation", nobox) ///
	graphregion(color(white)) bgcolor(white) 


gr combine obs ///
	, imargin(large) name(obs,replace) ///
	graphregion(color(white)) 

	gr export pie_source_2010.eps, replace
	gr export pie_source_2010.pdf, replace
gr close



************************************************************
************************************************************
*** Cyclicality figures 
*** Figures 2, 3, 4, 5

clear 
use `a1'
** may need to install following first: ssc install binscatter 

***Generate same sample selection as for regressions 
*** Drop obs with missing cat variables 
drop if missing(tercile)|missing(level1)|missing(ntmaj5) 

qui reghdfe exp_dhs rev_dhs perinc_dhs , noabsorb
	gen regsample = e(sample)
	
** Drop unnecessary obs. to save on memory
keep if regsample 

local bins 50

local rev "Revenue"
local exp "Expenditure"
local ass "Assets"
local liab "Liabilities"

local i = 0

foreach s in all {
foreach var in rev exp ass liab {
	binscatter `var'_dhs perinc_dhs ///
	if `s'==1 /// 
	,  nq(`bins') name(cycle_`var',replace) ///
	xtitle("Income Growth") ytitle("``var'' Growth") ylabel(,format(%9.2fc)) ///
	graphregion(color(white)) bgcolor(white) lcolor(black) mcolor(gray%50)
	
	gr export binscatter_ein_`var'_`s'.pdf,replace
	gr export binscatter_ein_`var'_`s'.eps,replace
	gr close
	
}	
}


*******************************************************
****** Histograms
*** Figure 1

foreach d in  score    { 

clear
clear matrix
use `raw_class'

local xscore "Desired Counter-Cyclicality Rating (DCR)"

twoway histogram `d' /// 
	,percent width(0.1) gap(10) ///
	ytitle("Percent ", size(medium)) ///
	ylabel( 0 1 2 3 4 5 , nogrid labsize(medium))  ///
	xtitle("`x`d'' ", size(large))  ///
	color(gray%50) lwidth(0)  discrete /// 
	graphregion(color(white)) bgcolor(white) 
		
graph display 
			
graph export graph_`d'.pdf, replace
graph export graph_`d'.eps, replace

	}
